package com.path.demo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
/**
 * 
 * @author aizhimin
 * 新浪微博：weibo.com/aizhimin  欢迎互粉交流
 *
 */
public class TestPathActivity extends Activity implements OnClickListener{
	
	private ImageView ivComposer;
	private RotateAnimation clockwiseAm;
	private RotateAnimation anticlockwiseAm;
	
	private ImageView ivCamera;
	private TranslateAnimation cameraOutTA;//相机
	private TranslateAnimation cameraInTA;
	
	private ImageView ivWith;
	private TranslateAnimation withOutTA;
	private TranslateAnimation withInTA;
	
	private ImageView ivPlace;
	private TranslateAnimation placeOutTA;
	private TranslateAnimation placeInTA;
	
	private ImageView ivMusic;
	private TranslateAnimation musicOutTA;
	private TranslateAnimation musicInTA;
	
	private ImageView ivThought;
	private TranslateAnimation thoughtOutTA;
	private TranslateAnimation thoughtInTA;
	
	private ImageView ivSleep;
	private TranslateAnimation sleepOutTA;
	private TranslateAnimation sleepInTA;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findView();
    }
    
    private void findView(){
    	ivComposer = (ImageView)this.findViewById(R.id.ivComposer);
    	ivComposer.setOnClickListener(this);
//    	am = new RotateAnimation ( 0, 360, 13, 13 );
    	clockwiseAm = new RotateAnimation ( 0, +360, 
    			Animation.RELATIVE_TO_SELF, 0.5f, 
    			Animation.RELATIVE_TO_SELF, 0.5f ); 
    	clockwiseAm.setDuration (300);
	    LinearInterpolator lin = new LinearInterpolator(); 
	    clockwiseAm.setInterpolator(lin); 
	    clockwiseAm.setAnimationListener(clockwiseAmListener);
	    
	    anticlockwiseAm = new RotateAnimation ( 0, -360, 
    			Animation.RELATIVE_TO_SELF, 0.5f, 
    			Animation.RELATIVE_TO_SELF, 0.5f ); 
	    anticlockwiseAm.setDuration (300);
	    anticlockwiseAm.setInterpolator(lin); 
	    anticlockwiseAm.setAnimationListener(anticlockwiseAmListener);
	    
	    ivCamera = (ImageView)this.findViewById(R.id.ivCamera);
	    ivCamera.setOnClickListener(this);
	    cameraOutTA = new TranslateAnimation(Animation.ABSOLUTE, -5.0f,  Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE,   240.0f, Animation.ABSOLUTE, 10.0f);
	    cameraOutTA.setDuration(200);
	    cameraInTA = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,  Animation.ABSOLUTE, -5.0f, Animation.ABSOLUTE,   10.0f, Animation.ABSOLUTE, 240.0f);
	    cameraInTA.setDuration(200);
	    
	    ivWith = (ImageView)this.findViewById(R.id.ivWith);
	    ivWith.setOnClickListener(this);
	    withOutTA = new TranslateAnimation(Animation.ABSOLUTE, -75f,  Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE,   225.0f, Animation.ABSOLUTE, 10.0f);
	    withOutTA.setDuration(200);
	    withInTA = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,  Animation.ABSOLUTE, -75f, Animation.ABSOLUTE,   10.0f, Animation.ABSOLUTE, 225.0f);
	    withInTA.setDuration(200);
	    
	    ivPlace = (ImageView)this.findViewById(R.id.ivPlace);
	    ivPlace.setOnClickListener(this);
	    placeOutTA = new TranslateAnimation(Animation.ABSOLUTE, -135f,  Animation.ABSOLUTE, -10.0f, Animation.ABSOLUTE,   190f, Animation.ABSOLUTE, 10.0f);
	    placeOutTA.setDuration(200);
	    placeInTA = new TranslateAnimation(Animation.ABSOLUTE, -10.0f,  Animation.ABSOLUTE, -135f, Animation.ABSOLUTE,   10.0f, Animation.ABSOLUTE, 190f);
	    placeInTA.setDuration(200);
	    
	    ivMusic = (ImageView)this.findViewById(R.id.ivMusic);
	    ivMusic.setOnClickListener(this);
	    musicOutTA = new TranslateAnimation(Animation.ABSOLUTE, -175f,  Animation.ABSOLUTE, -10.0f, Animation.ABSOLUTE,   130f, Animation.ABSOLUTE, 10.0f);
	    musicOutTA.setDuration(200);
	    musicInTA = new TranslateAnimation(Animation.ABSOLUTE, -10.0f,  Animation.ABSOLUTE, -175f, Animation.ABSOLUTE,   10.0f, Animation.ABSOLUTE, 130f);
	    musicInTA.setDuration(200);
	    
	    ivThought = (ImageView)this.findViewById(R.id.ivThought);
	    ivThought.setOnClickListener(this);
	    thoughtOutTA = new TranslateAnimation(Animation.ABSOLUTE, -205f,  Animation.ABSOLUTE, -10.0f, Animation.ABSOLUTE,   70f, Animation.ABSOLUTE, 10.0f);
	    thoughtOutTA.setDuration(200);
	    thoughtInTA = new TranslateAnimation(Animation.ABSOLUTE, -10.0f,  Animation.ABSOLUTE, -205f, Animation.ABSOLUTE,   10.0f, Animation.ABSOLUTE, 70f);
	    thoughtInTA.setDuration(200);
	    
	    ivSleep = (ImageView)this.findViewById(R.id.ivSleep);
	    ivSleep.setOnClickListener(this);
	    sleepOutTA = new TranslateAnimation(Animation.ABSOLUTE, -215f,  Animation.ABSOLUTE, -10.0f, Animation.ABSOLUTE,   5f, Animation.ABSOLUTE, 10.0f);
	    sleepOutTA.setDuration(200);
	    sleepInTA = new TranslateAnimation(Animation.ABSOLUTE, -10.0f,  Animation.ABSOLUTE, -215f, Animation.ABSOLUTE,   10.0f, Animation.ABSOLUTE, 5f);
	    sleepInTA.setDuration(200);
    }

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.ivComposer:
			if(isClockwise){
				ivComposer.startAnimation(clockwiseAm);
				ivCamera.startAnimation(cameraOutTA);
				ivCamera.setVisibility(View.VISIBLE);
				
				withOutTA.setStartOffset(20);
				ivWith.startAnimation(withOutTA);
				ivWith.setVisibility(View.VISIBLE);
				
				placeOutTA.setStartOffset(40);
				ivPlace.startAnimation(placeOutTA);
				ivPlace.setVisibility(View.VISIBLE);
				
				musicOutTA.setStartOffset(60);
				ivMusic.startAnimation(musicOutTA);
				ivMusic.setVisibility(View.VISIBLE);
				
				thoughtOutTA.setStartOffset(80);
				ivThought.startAnimation(thoughtOutTA);
				ivThought.setVisibility(View.VISIBLE);
				
				sleepOutTA.setStartOffset(100);
				ivSleep.startAnimation(sleepOutTA);
				ivSleep.setVisibility(View.VISIBLE);
			}else{
				ivComposer.startAnimation(anticlockwiseAm);
				
				ivSleep.startAnimation(sleepInTA);
				ivSleep.setVisibility(View.GONE);
				
				thoughtInTA.setStartOffset(20);
				ivThought.startAnimation(thoughtInTA);
				ivThought.setVisibility(View.GONE);
				
				musicInTA.setStartOffset(40);
				ivMusic.startAnimation(musicInTA);
				ivMusic.setVisibility(View.GONE);
				
				placeInTA.setStartOffset(60);
				ivPlace.startAnimation(placeInTA);
				ivPlace.setVisibility(View.GONE);
				
				withInTA.setStartOffset(80);
				ivWith.startAnimation(withInTA);
				ivWith.setVisibility(View.GONE);
				
				cameraInTA.setStartOffset(100);
				ivCamera.startAnimation(cameraInTA);
				ivCamera.setVisibility(View.GONE);
			}
			break;
		case R.id.ivCamera:
			
			break;
		case R.id.ivWith:
			
			break;
		case R.id.ivPlace:
			
			break;
		case R.id.ivThought:
			
			break;
		case R.id.ivMusic:
			
			break;
		case R.id.ivSleep:
			
			break;
		}
	}
	private boolean isClockwise = true;
	private AnimationListener clockwiseAmListener = new AnimationListener() {
		public void onAnimationEnd(Animation arg0) {
			Matrix matrix = new Matrix(); 
			matrix.setRotate(45); 
			Bitmap source = BitmapFactory.decodeResource(TestPathActivity.this.getResources(), R.drawable.composer_icn_plus);
			Bitmap resizedBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
			ivComposer.setImageBitmap(resizedBitmap);
			if(source!=null && !source.isRecycled())
				source.recycle();
			isClockwise = false;
		}
		public void onAnimationRepeat(Animation animation) {
		}
		public void onAnimationStart(Animation animation) {
		}
	};
	private AnimationListener anticlockwiseAmListener = new AnimationListener() {
		public void onAnimationEnd(Animation arg0) {
			Bitmap source = BitmapFactory.decodeResource(TestPathActivity.this.getResources(), R.drawable.composer_icn_plus);
			ivComposer.setImageBitmap(source);
			isClockwise = true;
		}
		public void onAnimationRepeat(Animation animation) {
		}
		public void onAnimationStart(Animation animation) {
		}
	};
}